//index.js
//获取应用实例
var app = getApp()
Page({
	data: {
		userInfo: {},
		todos: [],
		animationData: {},
		opacity: {},
		newTodo: 'newTodo',
		numberArray: [1, 2, 3, 4]
	},
	animation: null,
	onLoad: function() {
		var that = this;
		wx.request({
			url: 'http://localhost:51461/api/todo',
			data: {},
			method: 'GET',
			// header: {}, // 设置请求的 header
			success: function(res) {
				for (var i = 0; i < res.data; i++) {
					res.data[i].class = '';
				}
				that.setData({
					todos: res.data
				});
			},
			fail: function() {
				console.log("fail");
				// fail
			},
			complete: function() {
				console.log("compelete");
			}
		})

		var animation = wx.createAnimation({
			duration: 2000,
			timingFunction: 'ease', // "linear","ease","ease-in","ease-in-out","ease-out","step-start","step-end"
			delay: 0,
			transformOrigin: '50% 50% 0',
			success: function(res) {
				console.log(res);
			}
		})

		this.animation = animation;

		// this.rotateThenScale();
		//调用应用实例的方法获取全局数据
		app.getUserInfo(function(userInfo) {
			//更新数据
			that.setData({
				userInfo: userInfo
			})
		})
	},
	rotateThenScale: function() {
		this.animation.rotate(180).scale(3, 3).step()
		this.animation.translate3d(-100, 0, 100).step({
			duration: 10000
		});
		this.setData({
			animationData: this.animation.export()
		})
	},
	bindClick: function(e) {
		if (e.detail.value.newTodo.length === 0) return;
		var that = this;
		var newTodos = [{
			name: e.detail.value.newTodo,
			class: 'opacity'
		}].concat(that.data.todos);
		this.setData({
			todos: newTodos,
			newTodo: ''
		})
		this.animation.opacity(1).height('100%').step();

		that.setData({
			opacity: this.animation.export()
		})

	},
	removeItem: function(e) {
		console.log(e);
		var index = e.target.dataset.index;
		var todos = this.data.todos;
		todos.splice(index, 1);
		this.setData({
			todos: todos
		});
	}
});

var mySon = {

	name: function() {

	}
};